#region CSharp
using System;
using System.Collections;
using System.Diagnostics;

using CStock;
#endregion

namespace CStock
{
	
	/// <summary>
	/// Summary description for Portfolios.
	/// </summary>
    public class DataItems : StockObjectCollection, IDataItems
	{
		#region Constructor

		public DataItems() : base()
		{
		}

		#endregion

	
		#region ArrayList Override
		
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public int Count
		{
			get
			{
				return this.items.Count;
			}
		}

      
		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public int Add(IDataItem obj, IPortfolio parent)
		{
			System.Diagnostics.Debug.Assert(parent != null, "parent portfolio of item to add is null ");

			IDataItem newDataItem = (IDataItem)obj;

            System.Diagnostics.Debug.Assert(newDataItem != null, "data class to add in portfolio is null ");

            newDataItem.Parent = parent;
            if (base.Items.Contains(newDataItem))
            {
                ((StockObjectBase)newDataItem).Delete();
                newDataItem.ID = newDataItem.ID + "1";
                //newDataItem.Serialize();
               


            }
            base.Items.Add(newDataItem);
			return 0;

		}
		
		public void Remove(IDataItem obj)
		{
			IDataItem removePortfolio=(IDataItem)obj;
			if (this.items.Contains(removePortfolio))
			{
				this.items.Remove(removePortfolio);
				//OnChanged(EventArgs.Empty);
			}
		}
        public IDataItem DataItemPerSymbol(string mysymbol)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i].Symbol == mysymbol)
                    return this[i];
            }
            return null;


        }
        public int IndexOf(object obj)
		{
			return this.items.IndexOf(obj);
		}
		#endregion

		#region Implementation
		
		/// <summary>
		/// 
		/// </summary>
		//[System.Xml.Serialization.XmlIgnoreAttribute()]
        public new IDataItem this[string myID]
		{
			get
			{
				for(int i = 0; i < this.items.Count; i ++)
				{
					IDataItem myPf = base[i]  as IDataItem;
					if (myPf.DataClass.ID == myID)
						return myPf;
				}
				return null;
			}
			set
			{
                for (int i = 0; i < this.items.Count; i++)
				{
					IDataItem myPf = base[i]  as IDataItem;
					//if( myPf.Name == name)
                    if (myPf.DataClass.ID == myID)
					{
						base[i] = value;
					}
				}
			}
		}

		public new IDataItem this[int index]
		{
			get
			{
				try
				{
                    if ((index >= 0) && (index < this.items.Count))
					{
						object o = base[index];
						return o as IDataItem;
					}
					else
					{
						return null;
					}
				}
				catch
				{
					Debug.Assert(false,"CPortfolios","Error getting Portfolio");
					return null;
				}
			}
			set
			{
                if ((index >= 0) && (index < this.items.Count))
				{
					base[index] = value;
				}
			}
		}
		#endregion

	}
	
}
